﻿﻿using System.Collections;
using System.Collections.Generic;
using TBFramework;
using UnityEngine;
using DG.Tweening;
using System;
using UIController;
using UnityEngine.UI;
using StandardStorage;
using System.Threading.Tasks;
public partial class RunningMethod : BaseMethodClass
{
   
   StepProperty stepProperty = new StepProperty();
   public void Init(StepProperty _stepProperty)
   {
      stepProperty = _stepProperty;
   }


   public IEnumerator PlayMovie(Movie movie)
   {
     // print(movie.videoPlayer.name);
      yield return new WaitForSeconds(1);
   }
   
   /// <summary>
   /// 翻页轮播图
   /// </summary>
   /// <param name="carouselImage">图片属性</param>
   /// <returns></returns>
   public IEnumerator DisplayPictures(GameObject CarouselImageUIParent,CarouselImage carouselImage)
   {
      
      CarouselImageUIParent.SetActive(true);
      UIParentClass carouselImageControl = (UIParentClass)CarouselImageUIParent.GetComponent<CarouselImageComponent>();
      carouselImageControl.Init(carouselImage.t2d,value=>carouselImage.isOver=value);
      carouselImage.isOver = false;
      
     /* for (int i = 0; i < carouselImage.rawImage.Length; i++)
          {
             carouselImage.rawImage[i].transform.localRotation = Quaternion.Euler(0, 0, 0);
             carouselImage.rawImage[i].transform.SetSiblingIndex(carouselImage.rawImage.Length-i-1);
          }

          carouselImage.CarouselImageUIParent.SetActive(true);
          carouselImage.rawImage[0].sprite = carouselImage.t2d[0];
          carouselImage.isButtonClick = false;
          carouselImage.turningPageNum = 0;
          carouselImage.currentPageNum = 0;
          carouselImage.isClickDis = -1;
          carouselImage.isOver = false;
          carouselImage.buttons[2].gameObject.SetActive(false);
          carouselImage.buttons[0].onClick.RemoveAllListeners();
          carouselImage.buttons[0].onClick.AddListener(() =>
          {
             if (!carouselImage.isButtonClick)
             {
                if (carouselImage.currentPageNum > 0)
                {
                   if (carouselImage.isClickDis == 1)
                   {
                      carouselImage.currentPageNum--;
                      carouselImage.turningPageNum--;
                      for (int i = 0; i < carouselImage.rawImage.Length; i++)
                      {
                         if (i != Math.Abs(carouselImage.turningPageNum + 1) % 3)
                         {
                            carouselImage.rawImage[i].transform.localRotation = Quaternion.Euler(0, 120, 0);
                         }
                         else
                         {
                            carouselImage.rawImage[Math.Abs(carouselImage.turningPageNum) % 3].transform.SetSiblingIndex(2);
                         }
                      }
                   }
                   carouselImage.isClickDis = 2;
                   carouselImage.rawImage[Math.Abs(carouselImage.turningPageNum % 3)].sprite = carouselImage.t2d[carouselImage.currentPageNum];
                   carouselImage.rawImage[Math.Abs(carouselImage.turningPageNum - 1) % 3].sprite = carouselImage.t2d[carouselImage.currentPageNum - 1];
                   carouselImage.currentPageNum--;
                   carouselImage.isButtonClick = true;
                   carouselImage.rawImage[carouselImage.turningPageNum % 3].transform.DOLocalRotate(new Vector3(0, 0, 0), 0.5f).OnComplete(() =>
                   {
                      for (int i = 0; i < carouselImage.rawImage.Length; i++)
                      {
                         if (i != Math.Abs(carouselImage.turningPageNum + 1) % 3)
                         {
                            carouselImage.rawImage[i].transform.localRotation = Quaternion.Euler(0, 120, 0);
                         }
                         else
                         {
                            carouselImage.rawImage[Math.Abs(carouselImage.turningPageNum) % 3].transform.SetSiblingIndex(2);
                         }
                      }
                      carouselImage.isButtonClick = false;
                   });
                   carouselImage.turningPageNum--;
                }
                else if (carouselImage.currentPageNum == 0)
                {
                   carouselImage.isButtonClick = true;
                   carouselImage.turningPageNum = 0;
                   carouselImage.isClickDis = -1;
                   carouselImage.rawImage[1].transform.localRotation = Quaternion.Euler(0, 0, 0);
                   carouselImage.rawImage[2].transform.localRotation = Quaternion.Euler(0, 0, 0);
                   carouselImage.rawImage[1].transform.SetSiblingIndex(1);
                   carouselImage.rawImage[2].transform.SetSiblingIndex(0);
                   carouselImage.rawImage[0].transform.DOLocalRotate(new Vector3(0, 0, 0), 0.5f).OnComplete(() =>
                   {
                      carouselImage.rawImage[0].transform.SetSiblingIndex(2);
                      carouselImage.isButtonClick = false;
                   });
                }
             }
          });

          carouselImage.buttons[1].onClick.RemoveAllListeners();
          carouselImage.buttons[1].onClick.AddListener(() =>
          {
             if (!carouselImage.isButtonClick)
             {
                if (carouselImage.currentPageNum < carouselImage.t2d.Length - 1)
                {
                   if (carouselImage.isClickDis == 2)
                   {
                      carouselImage.currentPageNum++;
                      carouselImage.turningPageNum++;

                      for (int i = 0; i < carouselImage.rawImage.Length; i++)
                      {
                         if (i != (carouselImage.turningPageNum - 1) % 3)
                         {
                            carouselImage.rawImage[i].transform.localRotation = Quaternion.Euler(0, 0, 0);
                         }
                         else
                         {
                            carouselImage.rawImage[carouselImage.turningPageNum % 3].transform.SetSiblingIndex(2);
                         }
                      }
                   }
                   carouselImage.isClickDis = 1;
                   carouselImage.rawImage[carouselImage.turningPageNum % 3].sprite = carouselImage.t2d[carouselImage.currentPageNum];
                   carouselImage.rawImage[(carouselImage.turningPageNum + 1) % 3].sprite = carouselImage.t2d[carouselImage.currentPageNum + 1];
                   carouselImage.currentPageNum++;
                   carouselImage.isButtonClick = true;
                   carouselImage.rawImage[carouselImage.turningPageNum % 3].transform.DOLocalRotate(new Vector3(0, 120, 0), 0.5f).OnComplete(() =>
                   {
                      for (int i = 0; i < carouselImage.rawImage.Length; i++)
                      {
                         if (i != (carouselImage.turningPageNum - 1) % 3)
                         {
                            carouselImage.rawImage[i].transform.localRotation = Quaternion.Euler(0, 0, 0);
                         }
                         else
                         {
                            carouselImage.rawImage[carouselImage.turningPageNum % 3].transform.SetSiblingIndex(2);
                         }
                      }
                      carouselImage.isButtonClick = false;
                      if (carouselImage.currentPageNum == carouselImage.t2d.Length - 1)
                      {
                         carouselImage.buttons[2].gameObject.SetActive(true);
                      }
                   });
                   carouselImage.turningPageNum++;
                }
             }
          });
          carouselImage.buttons[2].onClick.RemoveAllListeners();
          carouselImage.buttons[2].onClick.AddListener(() =>
          {
             carouselImage.isOver = true;
          });*/


      while (!carouselImage.isOver && !stepProperty.stepCurrentProperty.skip)
      {
         yield return null;
      }
      CarouselImageUIParent.SetActive(false);
     // print("轮播图播放结束");
      yield return new WaitForSeconds(1);
   }
/// <summary>
/// 工具使用方法
/// </summary>
/// <param name="tools"></param>
/// <returns></returns>
   MethodCollection<Transform> methodCollection = new MethodCollection<Transform>(5);
   public IEnumerator ToolsUse(Tools tools, ScoreDetermination changeValue)
   {  
      methodCollection["ToolsUse"] = (Trans) =>
      {
         changeValue.selectValue1 = Trans.name;
      };
      StandardProperty.Instance.cameraStruct.mouseActions = new List<Action<Transform>>();
      tools.toolsUIParent.SetActive(true);
      UIParentClass toolsComponent = (UIParentClass)tools.toolsUIParent.GetComponent<ToolsComponent>();
      toolsComponent.Init<object>(null, (id, name, Sprite) => { tools.toolId = id; tools.buttonName = name; tools.sprite = Sprite; });
      StandardProperty.Instance.followUI.isOpen = false;
      StandardProperty.Instance.followUI.followUI.GetComponent<Image>().sprite = null;
      while (true)
      {
         
         if (tools.sprite != null && StandardProperty.Instance.followUI.followUI.GetComponent<Image>().sprite == null)
         {

            StandardProperty.Instance.followUI.isOpen = true;
            StandardProperty.Instance.cameraStruct.mouseActions.Add(methodCollection["ToolsUse"]);
            StandardProperty.Instance.cameraStruct.isOpen = true;
         }
         changeValue.selectValue = tools.toolId;
         StandardProperty.Instance.followUI.followUI.GetComponent<Image>().sprite = tools.sprite;
         if ((changeValue.selectValue == changeValue.changeValue && changeValue.selectValue1 == changeValue.changeValue1)|| stepProperty.stepCurrentProperty.skip)
         {
            StandardProperty.Instance.followUI.isOpen = false;
            StandardProperty.Instance.followUI.followUI.GetComponent<Image>().sprite = null;
            StandardProperty.Instance.cameraStruct.mouseActions.Remove(methodCollection["ToolsUse"]);
            methodCollection["ToolsUse"] = null;
            StandardProperty.Instance.cameraStruct.isOpen = false;
         //   print("已完成工具点击后点击物体");
            tools.toolsUIParent.SetActive(false);
            yield break;
         }
         yield return null;
      }      
   }

   /// <summary>
   /// 提示信息方法
   /// </summary>
   /// <param name="content"></param>
   /// <param name="duration"></param>
   /// <param name="isParentHidden"></param>
   /// <param name="isWait"></param>
   /// <returns></returns>


   public IEnumerator PromptInformationIE(string content, float duration, bool isParentHidden, float waitTime, bool isWait = true)
   {
      bool isInit = false;
      var comm = stepProperty.stepCurrentProperty.promptInformationObj.promptInformationUIParent.GetComponent<PromptInformationComponent>();
      yield return comm.Init(content, duration, isParentHidden,waitTime);
      while (!isInit && isWait && !stepProperty.stepCurrentProperty.skip)
      {
         isInit = comm.ReturnIsOver();
         yield return new WaitForEndOfFrame();
      }

   }



   public IEnumerator MultipleChoiceQuestion(GameObject MultipleChoiceQuestionUIParent, MultipleChoiceQuestion multipleChoiceQuestion)
   {
      
      //yield return PromptInformationIE("dddddd",2,true,false);


      MultipleChoiceQuestionUIParent.SetActive(true);
      Action<string> func = (s) =>
      {
         multipleChoiceQuestion.selectAnswer = s;
      };
      MultipleChoiceQuestionUIParent.GetComponent<MultipleChoiceQuestionComponent>().Init(multipleChoiceQuestion, func);

      while (multipleChoiceQuestion.selectAnswer == "" && !stepProperty.stepCurrentProperty.skip)
      {
         yield return null;
      }
      MultipleChoiceQuestionUIParent.SetActive(false);
      if (multipleChoiceQuestion.selectAnswer == multipleChoiceQuestion.correctAnswer)
      {
         print("判定为正确");
      }
      else
      {
         print("判定为错误");
      }
      yield return null;

   }



   public IEnumerator PictureOptions(GameObject PictureOptionsUIParent, PictureOptions pictureOptions)
   {
      PictureOptionsUIParent.SetActive(true);
      Action<bool> func = (s) =>
      {
         pictureOptions.isOver = s;
      };
      PictureOptionsUIParent.GetComponent<PictureOptionsComponent>().Init(pictureOptions, func);

      while (pictureOptions.isOver == false && !stepProperty.stepCurrentProperty.skip)
      {
         yield return null;
      }
      PictureOptionsUIParent.SetActive(false);     
      yield return null;
   }


   public void StopAll(Coroutine coro)
   {
      StandardProperty.Instance.step.Clear();
      if (coro != null)
      {
         StopCoroutine(coro);
      }
      StopAllCoroutines();
   }
}


